<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;

class AccountController extends Controller {

            public function company(){
                Session::get('role_id');
                Session::get('session_id');
                if(Session::get('role_id') == 1 ) {
                    //获取公司的账号
                    $info = DB::table('company')->select('id', 'contact', 'contact_mobile', 'name', 'address_info', 'created_at')->get();
                }else if(Session::get('role_id') == 2 ){
                    $info = DB::table('company')->where('id',Session::get('session_id'))
                        ->select('id', 'contact', 'contact_mobile', 'name', 'address_info', 'created_at')->get();
                }else if(Session::get('role_id') == 3 ){
                    $info = DB::table('company')->where('id',Session::get('company_id'))
                        ->select('id', 'contact', 'contact_mobile', 'name', 'address_info', 'created_at')->get();
                }

                return view('admin.account.company',['info'=>$info]);

            }

            public function son(){
                if(Session::get('role_id') == 1 ) {
                    //获取公司的账号
                    $info = DB::table('son')->select('id', 'contact', 'contact_mobile', 'name', 'created_at')->get();
                }else if(Session::get('role_id') == 2 ){
                    $info = DB::table('son')->where('company_id',Session::get('session_id'))
                        ->select('id', 'contact', 'contact_mobile', 'name', 'created_at')->get();
                }else if(Session::get('role_id') == 3 ){
                    $info = DB::table('son')->where('company_id',Session::get('company_id'))
                        ->select('id', 'contact', 'contact_mobile', 'name', 'created_at')->get();
                }
                return view('admin.account.son',['info'=>$info]);
            }

            /*
             *查看
             * */
            public function order(){

                if(Session::get('role_id') == 1 ) {
                    $order = DB::table('order')
                        ->select('*')
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }else if(Session::get('role_id') == 2 ){
                       $servicer = DB::table('servicer')
                           ->select('id')
                           ->where('company_id',Session::get('session_id'))
                            ->get();
                       $servicer_id = [];
                       foreach ($servicer as $key=>$value){
                           $servicer_id[] = $value->id;
                       }
                    $order = DB::table('order')
                        ->select('*')
                        ->whereIn('servicer_id',$servicer_id)
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }else if(Session::get('role_id') == 3 ){
                    $order = DB::table('order')
                        ->select('*')
                        ->where('company_id',Session::get('company_id'))
                        ->orderBy('id', 'desc')
                        ->paginate(10);
                }

                return view('admin.account.order',['order'=>$order]);
            }

            /*
             * 查看订单合同
             * */
            public function order_agreement(Request $request){

               $agreement = DB::table('agreement')
                   ->select('status','sign_agreement_id','order_product_id')
                   ->where('id',$request->agreement_id)
                   ->first();

               if ($agreement->status == 0){
                   echo '<center><h5>业务员还没签订合同，无法查看</h5></center>';
                   exit;
               }else if($agreement->status == 1){
                  try{
                        $info = DB::table('sign_agreement')
                            ->where('id',$agreement->sign_agreement_id)->first();
                        $order_product_id = explode(',',$agreement->order_product_id);
                        $info = (array)$info;
                        if(!empty($info)) {
                            $product = DB::table('order_product')->whereIn('id', $order_product_id)->get();
                            $info['product'] = $product;
                            $info = (object)$info;

                            return view('admin.order.agreement', ['info' => $info]);
                        }else{
                            echo '<center><h5>合同为空，无法查看</h5></center>';
                        }
                   }catch (\Exception $e){
                    echo '<center><h5>合同有错误，无法查看</h5></center>';
                   }
               }


            }




            /*
             * 订单操作
             * */
            public function order_operate(Request $request){
                $order_id = $request->order_id;
                $status = $request->status;
                $code = $request->code;
                $order = DB::table('order')
                    ->where('id',$order_id)
                    ->select('code','status','agreement_status','pay_status')->first();
                if($order->code == 0 &&$order->status == 0 && $code ==1  ||
                   $order->code == 1 &&$order->status == 1 && $order->pay_status == 1 && $code ==3 ||
                   $order->code == 0 &&$order->status == 0 && $code == 2||$order->code == 1 &&$order->status == 1&& $code ==0&& $status ==0

                ){
                    DB::table('order')->where('id',$order_id)->update(array('code'=>$code,'status'=>$status));
                    echo "<script language='javascript'>alert('确认成功');window.location.href='/admin/order/order_find?code=0&&status=0&&preget=0';</script>";
                }else{
                    echo "<script language='javascript'>alert('确认失败');window.location.href='/admin/order/order_find?code=0&&status=0&&preget=0';</script>";
                }


            }


            public function delete(Request $request){
                $bool = DB::table('company')->where('id',$request->id)->delete();
                if($bool){
                    echo "<script language='javascript'>alert('删除成功');window.location.href='/admin/account/company';</script>";
                }else{
                   echo "<script language='javascript'>alert('删除失败');window.location.href='/admin/account/company';</script>";
                }
            }

            public function add(){

               return view('admin.account.add',[]);
            }

            //添加数量
            public function  add_process(Request $request){
                    if(Session::get('role_id') != 1 ) {
                        echo "<script language='javascript'>alert('必须要最高权限账号才能添加');window.location.href='/admin/account/company';</script>";
                        exit;
                    }
                    $info = $request->except('_token');
                    $info['date'] = date('Y-m-d H:i:s',time());
                    $info['created_at'] = time();
                    if($info['password'] == $info['repassword']){
                        unset($info['repassword']);
                        $info['password'] = Hash::make($info['password']);

                        try {
                            $bool = DB::table('company')->insert($info);
                            echo "<script language='javascript'>alert('添加成功');window.location.href='/admin/account/company';</script>";
                        }catch (Exception $e){
                           echo "<script language='javascript'>alert('添加失败,数据重复');window.location.href='/admin/account/company';</script>";
                        }
                    }else{

                            echo "<script language='javascript'>alert('密码不重复');window.location.href='/admin/account/company';</script>";
                    }
            }

            //子管理账号添加
            public function son_add(){

               return view('admin.account.son_add',[]);
            }



            public function son_add_process(Request $request){
                    $info = $request->except('_token');
                    $info['company_id'] = Session::get('session_id');
                    $info['date'] = date('Y-m-d H:i:s',time());
                    $info['created_at'] = time();
                    if($this->check($info['name'])==false ){
                        echo "<script language='javascript'>alert('账号非法');window.location.href='/admin/account/son';</script>";
                        return 0;
                    }
                    if($info['password'] == $info['repassword']){
                        unset($info['repassword']);
                        $info['password'] = Hash::make($info['password']);
                        try {
                             DB::table('son')->insert($info);
                             echo "<script language='javascript'>alert('添加成功');window.location.href='/admin/account/son';</script>";
                        }catch (Exception $e){
                           echo "<script language='javascript'>alert('添加失败');window.location.href='/admin/account/son';</script>";
                        }
                    }else{

                            echo "<script language='javascript'>alert('密码不重复');window.location.href='/admin/account/son';</script>";
                    }
            }

            public function son_delete(Request $request){

                try {
                    $bool = DB::table('son')->where('id', $request->id)->delete();
                    echo "<script language='javascript'>alert('删除成功');window.location.href='/admin/account/son';</script>";
                }catch (Exception $e){
                    echo "<script language='javascript'>alert('删除失败');window.location.href='/admin/account/son';</script>";
                }
            }

            //子管理权限
            public function son_role(Request $request)
            {
                $id = $request->son_id;
                $all = DB::table('route')->get();
                $list = [];
                $top_list = [];
                foreach ($all as $key => $value) {
                    if ((int)$value->parent_id == 0) {
                        $top_list[$value->id]['id'] = $value->id;
                        $top_list[$value->id]['name'] = $value->name;
                        $top_list[$value->id]['path'] = $value->path;
                    } else {
                        $list[$value->parent_id][$value->id]['id'] = $value->id;
                        $list[$value->parent_id][$value->id]['name'] = $value->name;
                        $list[$value->parent_id][$value->id]['path'] = $value->path;
                    }
                }

                return view('admin.iframe.son_role', ['list' => $list, 'top_list' => $top_list, 'id' => $id]);

            }

            //子管理权限
            public function son_role_add(Request $request){
                $all = $request->all();
                DB::table('son_power')->where('son_id',$all['id'])->delete();
                $info = [];
                foreach ($all['option'] as $key => $value){
                      if($value == 1) {
                          $info[] = array('path' => $all['path'][$key], 'route_name' => $all['route_name'][$key], 'son_id' => $all['id'], 'route_id' => $all['route_id'][$key]);
                      }
                }
                $bool = DB::table('son_power')->insert($info);
                if($bool){
                    echo "<script language='javascript'>alert('设置成功');window.history.back(-1);</script>";
                }else{
                    echo "<script language='javascript'>alert('设置失败');window.history.back(-1);</script>";
                }
            }


            //检查账号有没有添加过
            public function check($username){

                $admin = DB::table('admin')->where('account','=',$username)->first();
                $company = DB::table('company')->where('contact','=',$username)->first();
                $son = DB::table('son')->where('name','=',$username)->first();
                if(isset($admin->id)||isset($company->id)||isset($son->id)){
                    return false;
                }else{
                    return true;
                }

            }



            /*
             * 查看账号操作日志
             * */
            public function oprate_log(){

                if(Session::get('role_id') == 1 ) {
                    //获取公司的账号
                    $oprate_log = DB::table('oprate_log')->select('*')->orderBy('id','desc')->paginate(15);
                }else if(Session::get('role_id') == 2 ){
                    $servicer = DB::table('son')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }
                    $servicer_id[] = Session::get('session_id');
                    $oprate_log = DB::table('oprate_log')->where('user_id',Session::get('session_id'))
                        ->select('*')->orderBy('id','desc')->paginate(15);
            }else if(Session::get('role_id') == 3 ){
                    $servicer = DB::table('son')
                       ->select('id')
                       ->where('company_id',Session::get('company_id'))
                        ->get();
                   $servicer_id = [];
                   foreach ($servicer as $key=>$value){
                       $servicer_id[] = $value->id;
                   }
                    $servicer_id[] = Session::get('session_id');
                    $oprate_log = DB::table('oprate_log')->where('user_id',Session::get('session_id'))
                        ->select('*')->orderBy('id','desc')->paginate(15);
            }

                return view('admin/account/oprate_log',['oprate_log'=>$oprate_log]);

            }






}



?>